package exceptions;

import javax.swing.*;
import java.awt.*;

/**
 * Some useful utilities, that can't be referred to other classes.<br>
 * <br>
 *
 * @author Mihail A. Muhin
 */
public class Util {

	/**
	 * Outputs error message for the error occured due to illegal code.<br>
	 * <br>
	 *
	 * @param ErrMsg error message
	 */
	public static void ShowDebugError(String ErrMsg) {
		System.out.println(ErrMsg);
	}

	/**
	 * Outputs error message, so that it is seen by user.<br>
	 * <br>
	 *
	 * @param ErrMsg error message
	 */
	public static void ShowFatalError(String ErrMsg) {
		LogError(ErrMsg);

		JDialog dlg = new JDialog();
		JLabel l = new JLabel("Abnormal program termination\n".concat(ErrMsg));

		l.setForeground(new Color(0, 0, 0));

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		dlg.setTitle("Fatal error");
		dlg.getContentPane().setLayout(new BorderLayout());
		dlg.getContentPane().add(l, BorderLayout.CENTER);
		dlg.pack();
		dlg.setSize(dlg.getWidth(), dlg.getHeight() + 30);
		dlg.setLocation((int) (d.getWidth() - l.getWidth()) / 2, (int) (d.getHeight() - l.getHeight()) / 2);
		dlg.setModal(true);
		dlg.setVisible(true);
	}

	/**
	 * Outputs error message to some logs.<br>
	 * <br>
	 *
	 * @param ErrMsg error message
	 */
	public static void LogError(String ErrMsg) {
		System.out.println(ErrMsg);
	}

	/**
	 * Outputs exception info.<br>
	 * <br>
	 *
	 * @param e - exception to print info about.
	 */
	public static void PrintException(Throwable e) {
		System.out.println("---Exception info");
		System.out.println("Exception class: ".concat(e.getClass().getName()));
		System.out.println("Error message: ".concat(e.getMessage()));
		System.out.println("-Stack");
		e.printStackTrace();
		System.out.println("-Stack end");
		System.out.println("---Exception info end");
	}
}
